package cn.dianhun.om.jpa.controller;

import cn.dianhun.om.jpa.pojo.SysMenuEntity;
import cn.dianhun.om.jpa.pojo.vo.QueryMenuVo;
import cn.dianhun.om.jpa.repository.MenuRepository;
import cn.dianhun.om.jpa.utills.R;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

/**
 * @author cyt
 * @create 2020-07-08 10:51
 */

@Slf4j
@RequestMapping("/api")
@RestController
public class testMenuController {

    @Autowired
    MenuRepository menuRepository;

    /**
     * ID 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public R deleteById(@PathVariable("id") long id) {
        menuRepository.deleteById(id);
        return R.ok();
    }

    /**
     * 插入数据
     *
     * @param sysMenuEntity
     * @return
     */
    @PostMapping("/insert")
    public R insert(@RequestBody SysMenuEntity sysMenuEntity) {
        menuRepository.save(sysMenuEntity);
        return R.ok();
    }

    /**
     * 查询全部
     */
    @GetMapping("/getAllMenu")
    public R getAllMenu() {
        List<SysMenuEntity> menuRepositoryAll = menuRepository.findAll();
        return R.ok().data("list", menuRepositoryAll);
    }

    /**
     * 按照名字查詢
     */
    @GetMapping("/getByname")
    public R getByname(@RequestParam("name") String name) {
        SysMenuEntity menuEntity = menuRepository.findByName(name);
        return R.ok().data("menu", menuEntity);
    }

    /**
     * 按照名字和類型查詢
     *
     * @param name
     * @param type
     * @return
     */
    @GetMapping("/getBynameAndtype")
    public R getBynameAndtype(@RequestParam("name") String name, @RequestParam("type") Integer type) {
        SysMenuEntity menuEntity = menuRepository.findByNameAndType(name, type);
        return R.ok().data("menu", menuEntity);
    }

    /**
     * 名字模糊查询
     *
     * @param name
     * @return
     */
    @GetMapping("/getBynamelike")
    public R getBynamelike(@RequestParam("name") String name) {
        List<SysMenuEntity> list = menuRepository.findByNameLike(name);
        return R.ok().data("menu", list);
    }

    /**
     * 分页查询
     *
     * @param numPage
     * @param size
     * @return
     */
    @GetMapping("/page/{numPage}/{size}")
    public R page(@PathVariable("numPage") Integer numPage, @PathVariable("size") Integer size) {
        Page<SysMenuEntity> page = menuRepository.findAll(PageRequest.of(numPage, size));
        int totalPages = page.getTotalPages();
        long totalElements = page.getTotalElements();
        List<SysMenuEntity> content = page.getContent();
        int number = page.getNumber();
        return R.ok().data("totalpages", totalPages).data("totalelements", totalElements).data("content", content).data("number", number);
    }

    /**
     * order_num 排序
     *
     * @return
     */
    @GetMapping("/sort")
    public R sortByorderNum() {
        List<SysMenuEntity> orderNum = menuRepository.findAll(Sort.by(Sort.Direction.ASC, "orderNum"));
        return R.ok().data("sort", orderNum);
    }


    /**
     * 多条件查询
     *
     * @param queryMenuVo
     * @return
     */
    @ApiOperation("多条件 分页查询")
    @PostMapping("/testQuery")
    public R testQuery(@RequestBody QueryMenuVo queryMenuVo, @RequestParam("size") Integer size, @RequestParam("page") Integer page) {

        Specification<SysMenuEntity> specification = (Specification<SysMenuEntity>) (root, query, builder) -> {

            List<Predicate> predicateList = new ArrayList<>();

            if (!StringUtils.isEmpty(queryMenuVo.getName())) {
                predicateList.add(builder.like(root.get("name"),
                        "%" + StringUtils.trimWhitespace(queryMenuVo.getName()) + "%"));
            }

            if (!StringUtils.isEmpty(queryMenuVo.getUrl())) {
                predicateList.add(builder.like(root.get("url"),
                        "%" + StringUtils.trimWhitespace(queryMenuVo.getUrl()) + "%"));
            }
            return builder.and(predicateList.toArray(new Predicate[0]));
        };

        // List<SysMenuEntity> all = menuRepository.findAll(specification);
        Page<SysMenuEntity> menuRepositoryAll = menuRepository.findAll(specification, queryPage(page, size));
        List<SysMenuEntity> all = menuRepositoryAll.getContent();
        return R.ok().data("info", all);
    }

    /**
     * 分页插叙
     *
     * @param page
     * @param size
     * @return
     */
    private Pageable queryPage(Integer page, Integer size) {
        return PageRequest.of(page, size,
                Sort.by(Sort.Direction.DESC, "menuId"));
    }
}
